<template>
    <div>
        添加分类
        <van-cell-group>
            <!--           绑定的字段名        字段名                   提示语 -->
        <van-field v-model="cour.name" label="分类名称:" placeholder="请输入分类名称" />
        <van-field v-model="cour.pid" label="父级id:" placeholder="请输入父级id" />
        <van-field v-model="cour.params" label="参数:" placeholder="请输入参数配置" />

        选择分类
        <reply :data='cates'/>

        <van-button @click="add">添加</van-button>
        </van-cell-group>
        <!-- {{cates}} -->
        分类展示
        <table>
            <tr>
                <td>id</td>
                <td>分类名称</td>
                <td>父级分类</td>
                <td>参数配置</td>
            </tr>
            <tr v-for="i in frm">
                <td>{{i.id}}</td>
                <td>{{i.name}}</td>
                <td>{{i.pid}}</td>
                <td>{{i.params}}</td>
            </tr>
        </table>
    </div>
</template>

<script>
    import reply from './reply'
    export default {
        components:{
            'reply':reply
        },
        data(){
            return{
                frm:{},
                cour:{'pid':0},
                cates:{}
            }
        },
        methods:{
            get_course(){
                this.$axios.get('/course/cateM').then(res=>{
                    this.frm = res.data.list

                })
            },
            add(){
                if(this.cour.params){
                    this.cour.params = JSON.stringify(this.cour.params)
                }
                if(localStorage.getItem('pid')){
                    this.pid = localStorage.getItem('pid')
                }
                let data = {'name':this.cour.name,'pid':this.cour.pid,'params':this.cour.params}
                this.$axios.post('/course/cateM',data).then(res=>{
                    if(res.data.code==200){
                        alert('添加成功')
                        this.get_course()
                    }else{
                        alert('添加失败')
                    }
                })
            },
            //获取无限级分类
            getlist(){
                this.axios.get('/course/cateM').then(res=>{
                    this.cates = {'id':0,'name':'','son':res.data.list}
                    var name =JSON.parse(res.data.list[4]['params'])
                    alert(name['name'])
                })
            }
        },
        mounted(){
            this.get_course()
            this.getlist()
        }
    }
</script>

<style scoped>

</style>